﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using DevExpress.XtraEditors.DXErrorProvider;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Repast
{
    public partial class Frm_PurchaseStorageOrderDetail : BaseXtraForm
    {
        #region 属性
        public event EventHandler SaveEvent;

        List<PurchaseStorageOrderDetail> DataList;
        #endregion
        public Frm_PurchaseStorageOrderDetail(EStatus status)
        {
            InitializeComponent();
            this.FStatus = status;
            this.DataList = new List<PurchaseStorageOrderDetail>();
            this.BindCheckEdit(gridView1, false, false);
            this.OrderNum.Text = "PS" + DateTime.Now.ToString("yyyyMMddhhmmss");
            this.StorageTime.DateTime = DateTime.Now;

            //加载数据
            var rservice = new RepastService();
            SupplierID.Properties.DataSource = rservice.GetAllSuppliers();
            repositoryItemSearchLookUpEdit1.DataSource = rservice.GetAllMaterial();
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_PurchaseStorageOrderDetail_Load(object sender, EventArgs e)
        {
            switch (this.FStatus)
            {
                case EStatus.Add:
                    this.Text = "采购入库单-添加";
                    break;
                case EStatus.Modify:
                    this.Text = "采购入库单-编辑";
                    break;
                case EStatus.Look:
                    this.Text = "采购入库单-查看";
                    break;
            }

        }

        /// <summary>
        /// 采购订货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_PurchaseStorageAdd frm = new Frm_PurchaseStorageAdd();
            frm.SaveEvent += StorageAdd_SaveEvent;
            frm.ShowDialog();
        }
        private void StorageAdd_SaveEvent(Object sender, EventArgs e)
        {
            var orderDetail = sender as List<PurchaseOrderDetail>;
            var storageDetail = new List<PurchaseStorageOrderDetail>();
            foreach (var item in orderDetail)
            {
                var detail = new PurchaseStorageOrderDetail
                {
                    MaterialID = item.MaterialID,
                    MaterialName = item.MaterialName,
                    Number = item.Number,
                    Unit = item.Unit,
                    Price = item.Price,
                    Money = item.Money,
                    Canteen = item.Canteen,
                    Stall = item.Stall,
                    Remark = item.Remark,
                };
                storageDetail.Add(detail);
            }

            this.DataList = storageDetail;
            gridControl1.DataSource = storageDetail;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            InitValidationRules();
            RepastService rservice = new RepastService();

            if (this.FStatus == EStatus.Add)
            {
                Guid headerId = SaveHeaderInfo();
                var list = (List<PurchaseStorageOrderDetail>)gridControl1.DataSource;
                rservice.AddStorageOrder(list,headerId);
            }
            else if (this.FStatus == EStatus.Modify)
            {

            }

            if (SaveEvent != null)
            {
                SaveEvent(null, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 保存采购入库主单信息方法
        /// </summary>
        /// <param name="billHeaderID"></param>
        /// <returns></returns>
        private Guid SaveHeaderInfo(Guid? billHeaderID = null)
        {
            PSContext _db = new PSContext();
            var info = new PurchaseStorageOrder();

            if (billHeaderID == null)
            {
                info = new PurchaseStorageOrder
                {
                    ID = Guid.NewGuid(),
                    OrderNum = OrderNum.Text,
                    SupplierID = (Guid)SupplierID.EditValue,
                    StorageTime = StorageTime.DateTime,
                    Style = Style.Text == "0" ? "现金" : "月结",
                    Remark = Remark.Text,
                    CreateTime = DateTime.Now,
                    Creator = "管理员",
                    ExamineTime = DateTime.Now,
                };
                _db.PurchaseStorageOrders.Add(info);
            }
            else
            {
                var header = _db.PurchaseStorageOrders.Find(billHeaderID);
                header.OrderNum = OrderNum.Text;
                header.SupplierID = (Guid)SupplierID.EditValue;
                header.StorageTime = StorageTime.DateTime;
                header.Style = Style.Text == "0" ? "现金" : "月结";
                header.Remark = Remark.Text;
            }
            _db.SaveChanges();
            return info.ID;
        }

        /// <summary>
        /// 判断各个数据是否为空方法
        /// </summary>
        private void InitValidationRules()
        {
            if (this.DataList.Count == 0)
            {
                ShowInfo("明细数据不能为空!");
                return;
            }
            if (OrderNum.Text.Equals(""))
            {
                ShowInfo("订单号不能为空!");
                return;
            }
            if (SupplierID.EditValue.Equals(""))
            {
                ShowInfo("供应商不能为空!");
                return;
            }
            if (Style.Text.Equals(""))
            {
                ShowInfo("结算方式不能为空!");
                return;
            }
        }

        /// <summary>
        /// 表格内容改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

        }

    }
}
